package com.kintreda.ieco_server.helper;

import com.kintreda.ieco_server.util.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * 区域数据采集
 *
 */
public class AreaSpider {
	
	public static final String URL = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/";
	
	public static Integer index = 0;
	public static StringBuffer areaBuffer = new StringBuffer(10000000);
	public static String sql = null;
	
	public static Document request(String url) throws IOException{
		Document doc = Jsoup.connect(url).timeout(5000).get();
		return doc;
	}
	//读取省
	public static void getProvince(){
		try {
			Document provincesDom = AreaSpider.request(URL + "index.html");
			Element table = provincesDom.selectFirst(".provincetable");
			Elements provinces = table.select(".provincetr a");
			String code = null;
			String name = null;
			String href = null;
			Element link = null;
			//读取省
			for(int i = 0; i < provinces.size(); i++){
				link = provinces.get(i);
				href = link.attr("href");
				code = href.replace(".html", "");
				name = link.text();
				index++;
				//if(StringUtils.equals(code, "12")){
				//	System.out.println(index + "读取省:" + code + "-" + name);
					sql = "insert into area(id,name,code,level,status) values("+index+",'"+name+"','"+code+"',1,1);\r\n";
					System.out.print(sql);
					areaBuffer.append(sql);
					getCity(code, href, index);
				//}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//读取市
	public static void getCity(String provinceCode,String url,Integer provinceIndex){
		try {
			Document cityDom = AreaSpider.request(URL + url);
			Elements cities = cityDom.select(".citytable .citytr");
			//读取市
			Elements links = null;
			Element codeLink = null;
			Element nameLink = null;
			String href = null;
			String code = null;
			String name = null;
			String cityCode = null;
			for(int i = 0; i < cities.size(); i++){
				links = cities.get(i).select("td a");
				codeLink = links.get(0);
				nameLink = links.get(1);
				
				href = codeLink.attr("href");
				code = codeLink.text();
				name = nameLink.text();
				cityCode = href.replace(".html", "").replace(provinceCode + "/", "");
				index++;
				sql = "	insert into area(id,name,code,pid,level,status) values("+index+",'"+name+"','"+code+"',"+provinceIndex+",2,1);\r\n";
				System.out.print(sql);
				areaBuffer.append(sql);
				//System.out.println(index + "	读取市:" + code + "-" + name);
				getCountry(provinceCode,cityCode,href,index);
			}
		} catch (IOException e) {
			getCity(provinceCode,url,provinceIndex);
		}
	}
	
	//读取县
	public static void getCountry(String provinceCode,String cityCode,String url,Integer cityIndex){
		try {
			Document countryDom = AreaSpider.request(URL + url);
			Elements countries = countryDom.select(".countytable .countytr");
			//读取县
			Elements rows = null;
			Elements links = null;
			Element codeLink = null;
			Element nameLink = null;
			String href = null;
			String code = null;
			String name = null;
			String countryCode = null;
			for(int i = 0; i < countries.size(); i++){
				rows = countries.get(i).select("td");
				links = rows.select("a");
				if(links == null || links.size() == 0){
					codeLink = rows.get(0);
					nameLink = rows.get(1);
					href = null;
				}else{
					codeLink = links.get(0);
					nameLink = links.get(1);
					href = codeLink.attr("href");
				}
				code = codeLink.text();
				name = nameLink.text();
				index++;
				sql = "		insert into area(id,name,code,pid,level,status) values("+index+",'"+name+"','"+code+"',"+cityIndex+",3,1);\r\n";
				System.out.print(sql);
				areaBuffer.append(sql);
				if(href != null){
					countryCode = href.replace(".html", "").replace(provinceCode + "/", "");
					getTown(provinceCode,cityCode,countryCode,href,index);
				}
			}
		} catch (IOException e) {
			getCountry(provinceCode,cityCode,url,cityIndex);
		}
	}
	//读取乡镇/街道
	public static void getTown(String provinceCode,String cityCode,String countryCode,String url,Integer countryIndex){
		try {
			Document townDom = AreaSpider.request(URL + provinceCode + "/" + url);
			Elements towns = townDom.select(".towntable .towntr");
			//读取乡镇/街道
			Elements rows = null;
			Elements links = null;
			Element codeLink = null;
			Element nameLink = null;
			String href = null;
			String code = null;
			String name = null;
			for(int i = 0; i < towns.size(); i++){
				rows = towns.get(i).select("td");
				links = rows.select("a");
				if(links == null || links.size() == 0){
					codeLink = rows.get(0);
					nameLink = rows.get(1);
					href = null;
				}else{
					codeLink = links.get(0);
					nameLink = links.get(1);
					href = codeLink.attr("href");
				}
				code = codeLink.text();
				name = nameLink.text();
				index++;
				sql="			insert into area(id,name,code,pid,level,status) values("+index+",'"+name+"','"+code+"',"+countryIndex+",4,1);\r\n";
				System.out.print(sql);
				areaBuffer.append(sql);
				if(href != null){
					countryCode = href.replace(".html", "").replace(provinceCode + "/", "");
					getVillage(provinceCode,cityCode,countryCode,code,href,index);
				}
			}
		} catch (IOException e) {
			getTown(provinceCode,cityCode,countryCode,url,countryIndex);
		}
	}
	//读取村委会
	public static void getVillage(String provinceCode,String cityCode,String countryCode,String townCode,String url,Integer townIndex){
		try{
			Document villageDom = AreaSpider.request(URL + provinceCode + "/" + cityCode.substring(2,4) + "/" + url);
			Elements villages = villageDom.select(".villagetable .villagetr");
			//读取村委会
			Elements rows = null;
			String code = null;
			String name = null;
			String subCode = null;
			for(int i = 0; i < villages.size(); i++){
				rows = villages.get(i).select("td");
				code = rows.get(0).text();
				subCode = rows.get(1).text();
				name = rows.get(2).text();
				index++;
				sql = "				insert into area(id,name,code,subCode,pid,level,status) values("+index+",'"+name+"','"+code+"','"+subCode+"',"+townIndex+",5,1);\r\n";
				System.out.print(sql);
				areaBuffer.append(sql);
			}
		} catch (IOException e) {
			getVillage(provinceCode,cityCode,countryCode,townCode,url,townIndex);
		}
	}
	public static void main(String[] args) {
		
		for(int i = 0; i < 10; i++){
			System.out.println(StringUtil.getUUID());
		}
		
		/*AreaSpider.getProvince();
		FileWriter writer = null;
		try {
			writer = new FileWriter(new File("D:\\area.txt"));
			writer.write(areaBuffer.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(writer != null){
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}*/
	}
}
